<template>
    <el-drawer  :title="title" visible @close="$emit('close');$emit('load')" size="1200px" :wrapperClosable="false">
        <!-- :modal-append-to-body="false" -->
        <div class="add-main" v-loading="loading">
            <div class="btn-con">
                <el-button v-if="handleShow != 'detail'" type="primary" size="small" icon="el-icon-circle-check"
                    @click.native.prevent="submit()" :disabled="loading">提交
                </el-button>
            </div>
            <receive ref="receive" :handleShow="handleShow" />
         <!-- <goods v-if="goodsShow" ref="goods" :handleShow="handleShow" :data="data" :suppId="suppId?String(suppId):null" />
            :pageType="'purchase'" :orderNo="orderNo" :suppId="suppId ? String(suppId) : null" -->
            <goods ref="goods" :handleShow="handleShow" />
        </div>
    </el-drawer>
</template>
<script>
import throttle from 'lodash.throttle'
import receive from "./detail/address.vue"
// import bill from "./detail/bill.vue"
import goods from "./detail/goods.vue"
// import enclosure from "./detail/enclosure.vue"
import {createOrder } from "@/api/purchaseOrder/purchase.js"
export default {
    name: '',
    props: {
        title: String,
        handleShow: {
            type: String, //add新增edit编辑
            default: ""
        },
        data: {
            type: Object,
            default: function () {
                return {}
            }
        }
    },
    components: {
        receive,
        goods,
    },
    data() {
        return {
            loading: false,
            GoodList:[],
            selectList:[],
            // goodsShow: false,
            // // goodsShow: true,
            // orderNo: this.data ? String(this.data.order_no) : null,
        }
    },
    created() {

    },
    methods: {
        close() {
            // this.$refs.basic.form = {}
            this.$refs.receive.form = {}
            // this.$refs.bill.form = {}
            // if (this.$refs.goods) {
            //     this.$refs.goods.form = {}
            // }
            // this.$refs.enclosure.subForm = {}
            this.$emit('close');
            this.$emit('load')
        },
        getGoods(id) {
            this.suppId = id
            this.goodsShow = true
            this.$nextTick(() => {
                this.$refs.goods.goodsList = []
            })
        },
        submit: throttle(function () {
                console.log(this.$refs.goods.selectList, "shuju")
                this.selectList = this.$refs.goods.selectList
                console.log(this.selectList,'999')
            let address_id = this.$refs.receive.form.war_id
                console.log(address_id, 'address id')
                if(!address_id){
                    this.$message.error('请选择收货地址');
                } else if(this.selectList.length == 0){
                    this.$message.error('请选择商品');
                }
                
                for (let i = 0; i < this.selectList.length; i++) {
                    let goodsInfo =  this.selectList[i]
                    // if(!goodsInfo.stock_sales){
                    //     this.$message.error('请输入商品数量');
                    // }
                    this.GoodList.push({
                        'goods_code':goodsInfo.code,
                        'goods_sku':goodsInfo.goods_sku,
                        'stock_sales':goodsInfo.stock_sales ? goodsInfo.stock_sales : 1
                    })
                }
            console.log(this.GoodList, 888)
            let params = {
                address_id:address_id,
                goods_json:JSON.stringify(this.GoodList)
            }
            createOrder(params).then(res => {
                    this.msgSuccess("操作成功！");
                    // this.loading = false;
                    this.close()
                }).catch(() => {
                    this.GoodList = []
                    console.log(this.GoodList,'000')
                    })
            // fun.push(
            //     new Promise((resolve, reject) => {
            //         this.$refs.goods.$refs.form.validate((valid) => {
            //             if (valid) {
            //                 resolve();
            //             } else reject();
            //         });
            //     })
            // )
            // Promise.all(fun).then(data => {
                // this.loading = true;
                // let form = {
                //     ...this.$refs.basic.form,
                //     ...this.$refs.receive.form,
                //     ...this.$refs.bill.form,
                //     ...this.$refs.goods.form,
                //     ...this.$refs.enclosure.subForm,
                // }
               
                // form.order_no = this.data ? this.data.order_no : null

                // setPurchase(form).then(res => {
                //     this.msgSuccess("操作成功！");
                //     this.loading = false;
                //     this.close()
                // })
            // })
        }, 1000, { trailing: false }),
    },
}

</script>
<style scoped lang='scss'></style>